<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
class PayOrder extends Controller{

    public function _initialize()
    {
        $id         =   session('user');

        if(!$id)
        {
            $this->redirect('login/login/index');
        }
    }

    public function index()
    {
        $code               =   session('code','');
        $status = input('status', 0);

        $where = '1=1';
        if($status > 0) {
            $where = 'a.status='.$status;
        }

        if(input('start'))
        {
            $where .= ' and a.create_time >'.strtotime(input('start').' 00:00:00');
        }

        if(input('end'))
        {
            $where .= ' and a.create_time <'.strtotime(input('end').' 00:00:00');
        }
        $count = db('pay_order')->alias('a')->where($where)->count();
        $total_money = db('pay_order')->alias('a')->field('sum(money) as total_money')->where($where)->where(['status'=>2])->find();
        if(input('key'))
        {
            $where          .=  ' and (b.username like  "%'.input('key').'%" or b.phone like "%'.input('key').'%")';
        }

        $list       =   db('pay_order')->alias('a')->join('ap_user b','a.uid=b.id')->field('a.*,b.username,b.phone')->where($where)->order('id desc')->paginate(10, false, [
            'query' => input()
        ]);

        return view('index',[
            'parentid'=>input('parentid'),
            'count'=>$count,
            'list'  =>  $list,
            'code'  =>  $code,
            'start' => input('start',''),
            'end' => input('end',''),
            'key' => input('key',''),
            'total_money' => $total_money['total_money']?$total_money['total_money']:0,
        ]);
    }

}
